package com.demo.util.excel.utils;

import com.demo.util.excel.beans.ExcelColumnVO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.validation.constraints.NotNull;
import java.util.Map;
import java.util.function.Function;

public class ExcelStyleUtil {

    public static XSSFCellStyle getHeaderStyle(@NotNull XSSFWorkbook workbook) {
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        XSSFFont font = workbook.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        cellStyle.setFont(font);
        cellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getCellStyle(@NotNull XSSFWorkbook workbook){
        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setWrapText(true);
        return cellStyle;
    }

    public static <T> XSSFCellStyle getHeaderCellStyle(
            ExcelColumnVO<T> column,
            Map<String, XSSFCellStyle> styleMap
    ) {
        XSSFCellStyle headerCellStyle = styleMap.get("headerStyle");
        if (column != null) {
            Function<ExcelColumnVO<T>, String> headerStyleFunc = column.getHeaderStyle();
            if (headerStyleFunc != null) {
                String headerStyleName = headerStyleFunc.apply(column);
                headerCellStyle = styleMap.getOrDefault(headerStyleName, headerCellStyle);
            }
        }
        return headerCellStyle;
    }

    public static <T> XSSFCellStyle getDataCellStyle(
            ExcelColumnVO<T> column,
            Map<String, XSSFCellStyle> styleMap,
            Function<T, String> rowCellStyle,
            T item
    ) {
        // 获取默认样式
        XSSFCellStyle cellStyle = styleMap.get("cellStyle");
        // 获取列配置数据样式
        Function<T, String> cellStyleFunc = column.getCellStyle();
        if (cellStyleFunc != null) {
            String cellStyleName = cellStyleFunc.apply(item);
            cellStyle = styleMap.getOrDefault(cellStyleName, cellStyle);
        }
        // 获取行样式
        if (rowCellStyle != null) {
            String rowCellStyleName = rowCellStyle.apply(item);
            cellStyle = styleMap.getOrDefault(rowCellStyleName, cellStyle);
        }
        return cellStyle;
    }

}
